from django.db import models

from course.models import Course
from home.utils.BaseModel import BaseModel
from user.models import User


# Create your models here.
class Coupons(BaseModel):
    name = models.CharField(max_length=100)
    starttime = models.DateTimeField()
    endtime = models.DateTimeField()
    price = models.IntegerField()
    condition = models.IntegerField()

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'coupons'


class UserCoupon(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    coupon = models.ForeignKey(Coupons, on_delete=models.CASCADE)

    def __str__(self):
        return self.id

    class Meta:
        db_table = 'usercoupon'


class Orders(BaseModel):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    ordercode = models.CharField(max_length=100)
    totalprice = models.DecimalField(max_digits=10, decimal_places=2)
    realprice = models.DecimalField(max_digits=10, decimal_places=2)
    paytype = models.IntegerField(choices=((0, '支付宝'), (1, '微信'), (2, '银行卡')))
    paytime = models.DateTimeField(null=True, blank=True)
    status = models.IntegerField(choices=((0, '待支付'), (1, '已支付'), (2, '已取消'), (3, '已评价')))
    score = models.IntegerField(null=True, blank=True)
    coupon = models.IntegerField(null=True, blank=True)
    paycode = models.CharField(null=True, blank=True, max_length=100)  # 支付编码

    class Meta:
        db_table = 'orders'

    def __str__(self):
        return self.ordercode


class OrderDetails(BaseModel):
    order = models.ForeignKey(Orders, on_delete=models.CASCADE)
    courseid = models.ForeignKey(Course, on_delete=models.CASCADE)
    coursename = models.CharField(max_length=100)
    courseprice = models.DecimalField(max_digits=10, decimal_places=2)
    cover = models.CharField(max_length=100)
    type = models.IntegerField()

    class Meta:
        db_table = 'orderdetails'

    def __str__(self):
        return self.coursename
